//
// server.cpp
// ~~~~~~~~~~
//
// Based on Asio Example code.
// The original code can be found in the AsioExampleCode folder.
// Comments in this file reflect changes made

#include <ctime>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

// Removed make_daytime_string() function

int main()
{
	try
	{
		boost::asio::io_service io_service;
		typedef boost::shared_ptr<tcp::socket> socket_ptr;

		//udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));
		tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), 13));
		char data[1024];
		for (;;)
		{
			boost::array<char, 1024> recv_buf;
			//udp::endpoint remote_endpoint;
			socket_ptr sock(new tcp::socket(io_service));
			a.accept(*sock);
			boost::system::error_code error;
			// Receive data, store it in recv_buf, store its size in len
			//size_t len = socket.receive_from(boost::asio::buffer(recv_buf),
								//remote_endpoint, 0, error);


			size_t length = sock->read_some(boost::asio::buffer(data),error);
			boost::asio::write(*sock, boost::asio::buffer(data, length));


			// Write out the received data
                        //std::cout.write(recv_buf.data(), len);

			if (error && error != boost::asio::error::message_size)
				throw boost::system::system_error(error);

			// Store the message in a buffer
			//std::string message(recv_buf.begin(), recv_buf.end());

			boost::system::error_code ignored_error;
			//socket.send_to(boost::asio::buffer(message),
						   //remote_endpoint, 0, ignored_error);
			// Flush the output by printing a new line
			std::cout << std::endl;
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	return 0;
}
